﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Linq;
using System.Linq;
using KC.Business.Net;
using KC.Business.Meta;
using KC.Business.Meta.Jobs;
using KC.DataAccess.MV6;

namespace KC.Tests
{
	[TestClass]
	public class Meta
	{
		#region Job Tests
	
		[TestMethod]
		public void BlogPostCleanupJobTest()
		{
			BlogCleanupJob job = new BlogCleanupJob();
			job.PreProcess();
			int id = job.sj.ID;
			Assert.AreEqual(job.sj.Status, JobStatus.PreProcessed.ToString());

			job.Process();
			Assert.AreEqual(job.sj.Status, JobStatus.Processed.ToString());

			job.PostProcess();
			using (MV6DataContext dc = new MV6DataContext())
			{
				ServiceJob sj = (from j in dc.ServiceJobs
								 where j.ID == id
								 select j).First();
				Assert.AreEqual(JobStatus.Done.ToString(), sj.Status);
				Assert.IsTrue(sj.dEnded > sj.dStarted);
				Assert.IsTrue(sj.dEnded < DateTime.Now);
			}
		}
		[TestMethod]
		public void DailyReportJobTest()
		{
			DailyReportJob job = new DailyReportJob();
			job.PreProcess();
			int id = job.sj.ID;
			Assert.AreEqual(job.sj.Status, JobStatus.PreProcessed.ToString());

			job.Process();
			Assert.AreEqual(job.sj.Status, JobStatus.Processed.ToString());

			job.PostProcess();
			using (MV6DataContext dc = new MV6DataContext())
			{
				ServiceJob sj = (from j in dc.ServiceJobs
								 where j.ID == id
								 select j).First();
				Assert.AreEqual(JobStatus.Done.ToString(), sj.Status);
				Assert.IsTrue(sj.dEnded > sj.dStarted);
				Assert.IsTrue(sj.dEnded < DateTime.Now);
			}
		}
		[TestMethod]
		public void HeuristicsJobTest()
		{
			HeuristicsJob job = new HeuristicsJob();
			job.PreProcess();
			int id = job.sj.ID;
			Assert.AreEqual(job.sj.Status, JobStatus.PreProcessed.ToString());

			job.Process();
			Assert.AreEqual(job.sj.Status, JobStatus.Processed.ToString());

			job.PostProcess();
			using (MV6DataContext dc = new MV6DataContext())
			{
				ServiceJob sj = (from j in dc.ServiceJobs
								 where j.ID == id
								 select j).First();
				Assert.AreEqual(JobStatus.Done.ToString(), sj.Status);
				Assert.IsTrue(sj.dEnded > sj.dStarted);
				Assert.IsTrue(sj.dEnded < DateTime.Now);
			}
		}
		[TestMethod]
		public void SearchStatusUpdateJobTest()
		{
			SearchStatusUpdateJob job = new SearchStatusUpdateJob();
			job.PreProcess();
			int id = job.sj.ID;
			Assert.AreEqual(job.sj.Status, JobStatus.PreProcessed.ToString());

			job.Process();
			Assert.AreEqual(job.sj.Status, JobStatus.Processed.ToString());

			job.PostProcess();
			using (MV6DataContext dc = new MV6DataContext())
			{
				ServiceJob sj = (from j in dc.ServiceJobs
								 where j.ID == id
								 select j).First();
				Assert.AreEqual(JobStatus.Done.ToString(), sj.Status);
				Assert.IsTrue(sj.dEnded > sj.dStarted);
				Assert.IsTrue(sj.dEnded < DateTime.Now);
			}
		}

		#endregion Job Tests
	}
}
